package demo1;

import java.util.Arrays;

/**
 * @Author liangzai
 * @Description:
 */

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//    public int compareTo(Student o){
//        return this.age-o.age;
//    }

    public int compareTo(Student o){
        return this.name.compareTo(o.name);
    }
}

public class Test {
    public static void mySortage(Student[] students){
        for (int i = 0; i < students.length-1; i++) {
            for (int j = 0; j < students.length-1-i; j++) {
                if(students[j].compareTo(students[j+1])>0){
                    Student tmp=students[j];
                    students[j]=students[j+1];
                    students[j+1]=tmp;
                }
            }
        }
    }

    public static void mySortname(Student[] students){
        for (int i = 0; i < students.length-1; i++) {
            for (int j = 0; j < students.length-1-i; j++) {
                if(students[j].compareTo(students[j+1])>0){
                    Student tmp=students[j];
                    students[j]=students[j+1];
                    students[j+1]=tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0]=new Student("liuzhi",20);
        students[1]=new Student("huxiangyu",18);
        students[2]=new Student("zhanglujie",19);

        System.out.println("交换前："+ Arrays.toString(students));

//        AgeComparator ageComparator = new AgeComparator();
//        Arrays.sort(students,ageComparator);
//        NameComparator nameComparator = new NameComparator();
//        Arrays.sort(students,nameComparator);
//        int ret = students[0].compareTo(students[1]);
//        System.out.println(ret);

        //mySortage(students);
        mySortname(students);
        System.out.println("交换后："+ Arrays.toString(students));

    }
}
